const db = require('../db/index');

exports.getOrderMessage=(req,res)=>{
  const {payment_status,order_status}=req.body
  let sql =''
  if(payment_status=== '' && order_status===''){
    sql ='SELECT * FROM orders'
    db.query(sql,(err,results)=>{
      return res.status(201).json({ code:0,data:results});
    })
  }
  if(payment_status==='' && order_status!==''){
    sql ='SELECT * FROM orders WHERE order_status = ?  ORDER BY order_id;'
    db.query(sql,[order_status],(err,results)=>{
      return res.status(201).json({ code:0,data:results});
    })
  }
  if(payment_status!==''&& order_status===''){
    sql ='SELECT * FROM orders WHERE payment_status = ?  ORDER BY order_id;'
    db.query(sql,[payment_status],(err,results)=>{
      return res.status(201).json({ code:0,data:results});
    })
  }
  if(payment_status!==''&& order_status!==''){
    sql ='SELECT * FROM orders WHERE payment_status = ? AND order_status = ?  ORDER BY order_id;'
  
    db.query(sql,[payment_status,order_status],(err,results)=>{
      if (err) {
        console.log(err);
        return res.status(500).json({ message: 'Database error' });
      }
      return res.status(201).json({ code:0,data:results});
    })
  }
}


exports.getUserName=(req,res)=>{
  const user_id =req.params.user_id
  const sql = `select full_name from users where user_id=${user_id}`
  db.query(sql,(err,results)=>{
    if (err) {
      console.log(err);
      return res.status(500).json({ message: 'Database error' });
    }
    return res.status(201).json({ code:0,data:results});
  })
}

exports.getRoomName=(req,res)=>{
  const room_id =req.params.room_id
  
  const sql = `select room_name from rooms where room_id=${room_id}`
  db.query(sql,(err,results)=>{
    if (err) {
      console.log(err);
      return res.status(500).json({ message: 'Database error' });
    }
    return res.status(201).json({ code:0,data:results});
  })
}

exports.delorderMessage=(req,res)=>{
  const order_id =req.params.order_id
  const sql=`DELETE FROM orders WHERE order_id=${order_id};`
  db.query(sql,(err,results)=>{
    if (err) {
      console.log(err);
      return res.status(500).json({ message: 'Database error' });
    }
    return res.status(201).json({ code:0,data:results});
  })
}

exports.delPaymentsMessage=(req,res)=>{
  const order_id =req.params.order_id
  const sql=`DELETE FROM payments WHERE order_id=${order_id};`
  db.query(sql,(err,results)=>{
    if (err) {
      console.log(err);
      return res.status(500).json({ message: 'Database error' });
    }
    return res.status(201).json({ code:0,data:results});
  })
}
////////////////////////////////////////////////////////////////优惠券
exports.getCouponsMessage=(req,res)=>{
  const sql='select * from Coupons'
  db.query(sql,(err,results)=>{
    if (err) {
      console.log(err);
      return res.status(500).json({ message: 'Database error' });
    }
    return res.status(201).json({ code:0,data:results});
  })
}

exports.delCouponsMessage=(req,res)=>{
  const coupon_code =req.params.coupon_code
  console.log(coupon_code);
  const sql='DELETE FROM coupons WHERE coupon_code =?'
  db.query(sql,coupon_code,(err,results)=>{
    if (err) {
      console.log(err);
      return res.status(500).json({ message: 'Database error' });
    }
    return res.status(201).json({ code:0});
  })
}
exports.insertCoupon=(req,res)=>{
  const {coupon_name,coupon_code,discount,status,} =req.body
  console.log(coupon_name,coupon_code,discount,status,);
  const sql='INSERT INTO coupons (coupon_name, coupon_code, discount, status) VALUES (?, ?, ?, ?);'
  db.query(sql,[coupon_name,coupon_code,discount,status],(err,results)=>{
    if (err) {
      console.log(err);
      return res.status(500).json({ message: 'Database error' });
    }
    return res.status(201).json({ code:0,datd:results});
  })
}

exports.updataCoupon =(req,res)=>{
  const {coupon_name,coupon_code,discount,status} =req.body
  console.log(coupon_name,coupon_code,discount,status);
  const sql='UPDATE coupons SET coupon_name= ?, discount= ?, status=? WHERE coupon_code = ? ;'
  db.query(sql,[coupon_name,discount,status,coupon_code],(err,results)=>{
    if (err) {
      console.log(err);
      return res.status(500).json({ message: 'Database error' });
    }
    return res.status(201).json({ code:0,datd:results});
  })
}